package cn.new7as.cas5s.ui.login;


import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.OnAdidReadListener;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;


import cn.new7as.cas5s.ui.easy.netapi.PointApi;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.ui.slideshow.MainActivity;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.anhjkfu.MobileVerifyView;
import cn.new7as.cas5s.tool.util.SharePreferenceUtils;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.databinding.ActivityVeriedBinding;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.LoginInApi;
import cn.new7as.cas5s.ui.easy.netapi.SendApi;


public class VeriedActivity extends BaseActivity {


    private ActivityVeriedBinding binding;
    int count;
    int vtime;

    int otpcount;
    int OtpDuration;
    int logincount;
    boolean isstar;
    long sta;
    long getsta;


    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityVeriedBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initV();
        getsta = System.currentTimeMillis();
        startMethod4();
        binding.backsds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.go(VeriedActivity.this, LogininActivity.class);

                finish();
            }
        });
    }

    String codes;
    String akj;
    String akjda;

    public void initV() {
        Adjust.getGoogleAdId(this, s -> akj = s);
        Adjust.getAdid(new OnAdidReadListener() {
            @Override
            public void onAdidRead(String s) {
                akjda = s + "";
            }
        });

        binding.send.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        String data = getIntent().getStringExtra("data");
        binding.welcome.setText(String.format("Please enter the verification code sent to  +91 %s to continue.", data));
        binding.loginPwd.setOnClickListener(v -> {
            Utils.goextra(VeriedActivity.this, PwdLoginActivity.class, data);
            finish();
        });
        binding.backsds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        binding.rlVeri.setTextWatcher(new MobileVerifyView.MobileTextWatcher() {
            @Override
            public void onTextChanged(String text) {
                if (!isstar) {
                    sta = System.currentTimeMillis();
                    isstar = true;
                }
                if (text.toString().length() == 6) {
                    otpcount++;
                    codes = text;
                    int i1 = 1000;
                    OtpDuration += (System.currentTimeMillis() - sta) / i1;
                    Log.e("v", vtime + "");
                    isstar = false;
                } else {

                }
            }


        });


        binding.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i = 60;
                send(data);

            }
        });
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logincount++;
                loginIn(data, codes);
            }
        });


    }

    void loginIn(String pho, String ver) {
        show();

        EasyHttp.post(this)
                .api(new LoginInApi(pho, ver, akj, akjda)).request(new OnHttpListener<HttpData<LoginInApi.LoginData>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<LoginInApi.LoginData> result) {

                        SharePreferenceUtils spUtils = new SharePreferenceUtils(VeriedActivity.this);
                        spUtils.putString("useruniquecode", result.getResult().token);
                        spUtils.putString("phonenum", result.getResult().mobile);
                        spUtils.putString("ucode", result.getResult().userCode);
                        point(result.getResult().token);

                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }

    private void point(String s) {
        long end = System.currentTimeMillis();
        PointApi.PointData pointData = new PointApi().new PointData();
        pointData.getOTP_click_num = count;
        pointData.getOTP_login_duration = vtime;
        pointData.OTP_duration = (int) ((end - getsta) / 1000);
        pointData.OTP_updatecount = otpcount;
        pointData.type = 0;
        pointData.register_login_click_num = logincount;
        pointData.register_mobile_updatecount = getIntent().getIntExtra("count", 1);
        pointData.register_mobile_duration = getIntent().getIntExtra("dur", 1);
        long total = getIntent().getLongExtra("total", 1);
        pointData.registerpage_stop_duration = (int) ((end - start + total) / 1000);
        pointData.frist_click_time = Utils.getdate(start);
        pointData.end_click_time = Utils.getdate(end);
        EasyHttp.post(this)
                .api(new PointApi(s, pointData)).request(new OnHttpListener<HttpData<Object>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<Object> result) {
                        Utils.go(VeriedActivity.this, MainActivity.class);
                        finish();
                        hide();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });

    }


    public static final int CHANGE_TIPS_TIMER_INTERVAL = 1000;
    private Handler mChangeTipsHandler = new Handler(Looper.getMainLooper());
    int i = 60;

    private void startMethod4() {
        count++;
        binding.send.setEnabled(false);
        Runnable mChangeTipsRunnable = new Runnable() {
            @Override
            public void run() {
                binding.send.setText(i + "S");
                i--;
                if (i == 0) {
                    binding.send.setEnabled(true);
                    binding.send.setText("Resend");
                    binding.send.setTextColor(getResources().getColor(R.color.theme));
                } else
                    mChangeTipsHandler.postDelayed(this, CHANGE_TIPS_TIMER_INTERVAL);
            }
        };

        mChangeTipsHandler.post(mChangeTipsRunnable);
    }

    void send(String pho) {
        EasyHttp.post(this)
                .api(new SendApi(pho, "1")).request(new OnHttpListener<Object>() {
                    @Override
                    public void onHttpSuccess(@NonNull Object result) {
                        binding.send.setTextColor(getResources().getColor(R.color.text));
                        startMethod4();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }
}